<template>
  <addEditBaseStudentView ref="addEditBaseStudentView" />
  <editStudentExcelFile ref="editStudentExcelFile" />
  <addFileView ref="addFileView" />
  <div class="app-table">
    <div class="app-body">
      <div class="app-search">
        <el-form :inline="true" class="demo-form-inline" v-model="form">
          <el-form-item label="学生姓名">
            <el-input placeholder="请输入学生姓名" v-model="form.name" style="width:200px;"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="serach_btn" type="primary" plain @click="onSubmit"
              @keyup.enter="searchEnter">搜索</el-button>
            <el-button class="add_btn" type="warning" plain @click="exportExcel"
              v-loading.fullscreen.lock="fullscreenLoading">下载数据</el-button>
          </el-form-item>

        </el-form>
      </div>
      <el-table size="default" :data="list" :row-class-name="tableRowClassName">
        <el-table-column prop="id" label="id" width="90" />
        <el-table-column prop="name" label="姓名" min-width="150" />
        <el-table-column prop="studentNumber" label="学号" min-width="120" />
        <el-table-column prop="idCard" label="身份证号" min-width="200" />
        <el-table-column prop="address" label="地市" min-width="300" />
        <el-table-column prop="socre6" label="竞赛意向" min-width="300" />

        <el-table-column label="操作" width="140px">
          <template style="display: block" #default="scope">
            <!-- <el-button class="edit_btn" size="small" type="success" @click="editBaseStudent(scope.row)">编辑
            </el-button> -->
            <el-button class="del_btn" size="small" type="danger" @click="deleteBaseStudent(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
        <template v-slot:empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-config-provider :locale="locale">
        <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
          @current-change="changeCurrentPage">
        </el-pagination>
      </el-config-provider>
    </div>
    <bottomView></bottomView>
  </div>
</template>
<script>
import addEditBaseStudentView from "@/components/baseInfo/addEditBaseStudent.vue";
import bottomView from "@/components/bottom.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage, ElLoading } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { formatTime1, decrypt } from "@/utils/dateFormat";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import { Loading } from "element-plus/es/components/loading/src/service";

export default {
  name: "BaseStudentListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 1,
      pageSize: 10,
      total: 0,
      limit: 10,
      fullscreenLoading: false,
      list: [],
      cityType: [
        { label: '石家庄' },
        { label: '秦皇岛' },
        { label: '张家口' },
        { label: '承德' },
        { label: '保定/定州' },
        { label: '邯郸' },
        { label: '廊坊' },
        { label: '辛集' },
        { label: '邢台' },
        { label: '雄安新区' },
        { label: '沧州/华油' },
        { label: '衡水' },
        { label: '唐山' },
      ],
      form: {
        name: "",
        payStatus: "",
        address: "",
      },
      user: {},
    };
  },
  components: {
    addEditBaseStudentView,
    addFileView, ElLoading, bottomView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.user = JSON.parse(sessionStorage.getItem('user'))
    this.getBaseStudentList();

  },
  watch: {
    $route() {

    },
  },
  methods: {
    serachHandle() {
      this.page = 1
      this.getBaseStudentList();
    },

    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getBaseStudentList();
      }
    },
    changePaySataus(value) {

      this.page = 1
      this.getBaseStudentList();
    },
    changeAddress(value) {
      console.log(value);
      value != undefined ? this.form.address = value : this.form.address = ""
      this.page = 1
      this.getBaseStudentList();
    },

    editBaseStudent(obj) {
      this.$refs.addEditBaseStudentView.title = "修改信息";
    },

    deleteBaseStudent(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/greenCardStudent/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getBaseStudentList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },

    reloadData() {
      this.page = 1;
      this.getBaseStudentList();
    },

    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量创建";
      this.$refs.addFileView.uploadApiUrl = "/javaApi/baseStudent/importExcelAndPayment";
    },



    exportExcel() { // 导出数据
      this.fullscreenLoading = true;
      httpService(
        "/javaApi/greenCardStudent/exportExcel",
        {
          campus: this.user.campus
        },
        "get"
      ).then((data) => {
        this.fullscreenLoading = false;
        if (data.code == 200) {
          window.open("https://wx.zhengzhong.cn" + data.message, "_blank");
        }
      });
    },

    onSubmit() {
      this.getBaseStudentList();
    },

    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getBaseStudentList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getBaseStudentList();
    },

    tableRowClassName({ row, rowIndex }) {
      return "";
    },

    async getBaseStudentList() {
      const data = await httpService(
        "/javaApi/greenCardStudent/list/" + this.page + "/" + this.pageSize,
        {
          campus: this.user.campus
        },
        "get"
      );

      if (data.list) {
        data.list.map(item => {
          if (item.idCard) {
            const start = item.idCard.slice(0, 4);
            const end = item.idCard.slice(-4);
            const middleLength = item.idCard.length - start.length - end.length;
            const middle = '*'.repeat(middleLength);
            item.idCard = start + middle + end;
          }
        })
      }

      this.list = data.list;
      this.total = data.total;
      this.pageCount = data.pages;
    }

  },
};
</script>
<style scoped>
.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 5px 10px 10px;
  /* width: 100%; */
  width: calc(100vw - 220px);
}

.app-search {}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 237px);

}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.export_btn {
  background-color: #FF7200;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.down_btn {
  background-color: #898989;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.edit_btn {
  background-color: #fff;
  color: #007EFF;
  border: none;
}

.del_btn {
  background-color: #fff;
  color: #FF0000;
  border: none;
}

.del_btn1 {
  color: #fff;
  background-color: #ff0000b4;
  border: none;
}
</style>
